package org.lzy.aibg.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

//@Configuration 用于替代xml配置文件
@Configuration
//@用于替代<component-scan />完成包扫描
@ComponentScan(basePackages = {"org.lzy.aibg"})
//用于替代<context:property-placeholder />完成外部配置文件注入
@PropertySource(value = {"classpath:jdbc.properties"})
public class SpringConfig {
//    @Value("${jdbc.driverClassName}")
//    private String driverClassName;
//    @Value("${jdbc.url}")
//    private String url;
//    @Value("${jdbc.username}")
//    private String username;
//    @Value("${jdbc.password}")
//    private String password;

//    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
//        <property name="driverClassName" value="${jdbc.driverClassName}"/>
//        <property name="url" value="${jdbc.url}"/>
//        <property name="username" value="${jdbc.username}"/>
//        <property name="password" value="${jdbc.password}"/>
//    </bean>
    @Bean
    public DruidDataSource dataSource(@Value("${jdbc.driverClassName}") String driverClassName,
                                        @Value("${jdbc.url}") String url,
                                        @Value("${jdbc.username}") String username,
                                        @Value("${jdbc.password}") String password){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

//    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
//        <property name="dataSource" ref="dataSource"/>
//    </bean>
    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource){
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource);
        return jdbcTemplate;
    }
}
